<?php
namespace Pay\Service\Wxpay;

use Pay\Service\Wxpay\ActionAbstract;

class Jsapi extends ActionAbstract
{

    /*
     * 如果没有参数config默认取自有的配置
     */
    public function __construct($config = false)
    {
        parent::__construct($config);
    }

    /*
     * 进行H5支付
     * 一些必要参数$params如下:
     * $params['price']:价格
     * $params['subject']:标题
     * $params['body']:支付信息,不能为空
     * $params['orderNumber']:订单编号,不能为空
     * $params['redirectUrl']:支付后的显示页面,可以为空字符
     * $params['code']:微信code码,不能为空
     */
    public function doJsapi($params)
    {
        $params['code'] = '1';
        $wxpay = new \Pay\Wxpay\GetOpenid();
        $openid = $wxpay->getData($this->config, $params['code']);
        if ($openid) {
            return false;
        }
        $params['nonceStr'] = \Pay\Service\Common\Randstr::human(16);
        $params['openid'] = $openid;
        $ret = $this->SignPay($params);
        
        //jsapi支付需要二次加密
        $signdRet['appId'] = $ret['appId'];
        $signdRet['timeStamp'] = (string)time();
        $signdRet['nonceStr'] = $params['nonceStr'];
        $signdRet['signType'] = 'MD5';
        $signdRet['package'] = 'prepay_id=' . $ret['payUrl'];

        $signdRet['paySign'] = $this->sign($signdRet);
        $signdRet['redirectUrl'] = $params['redirectUrl'];
        return $signdRet;
    }

    protected function SignPay($params)
    {
        if (!$params['subject'] || !$params['body']) {
            return false;
        }
        if (!$params['price'] || !$params['orderNumber']) {
            return false;
        }
        $scene = ['h5_info' => ['type' => 'h5_info', 'app_name' => $params['subject']]];
        $scene = json_encode($scene);
        $ip = \Pay\Service\Common\Ip::getClientIp();
        $param = [
            'appid' => $this->config['appId'],
            'mch_id' => $this->config['mchId'],
            'nonce_str'=> $params['nonceStr'],
            'body'=> $params['body'],
            'out_trade_no' => $params['orderNumber'],
            'total_fee'=> $params['price'],
            'openid' => $params['openid'],
            'spbill_create_ip' => $ip,
            'notify_url' => $this->config['notifyUrl'],
            'trade_type'=> 'JSAPI',
            'sign_type' => 'MD5',
            'scene_info'=> $scene,
        ];
        ksort($param);
        $str = '';
        foreach ($param as $key => $value) {
            $str .= $key.'='.$value.'&';
        }
        $signStr = $str.'key='.$this->config['key'];
        $sign = strtoupper(md5($signStr));
        $param['sign'] = $sign;

        $content = $this->arrayToXml($param);
        $ret['payUrl'] = $this->doPay($content);
        $ret['appId'] = $this->config['appId'];
        return $ret;
    }

    protected function doPay($data)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_TIMEOUT, 30000);
        curl_setopt($curl, CURLOPT_URL, $this->config['payUrl']);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        $res = curl_exec($curl);
        curl_close($curl);
        $ret = $this->xmlToArray($res);
        return $ret['return_code'] == 'SUCCESS' ? $ret['mweb_url'] : '';
    }
}
?>
